package com.changgou.order.controller;

import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.order.feign.CartFeign;
import com.changgou.order.feign.OrderFeign;
import com.changgou.order.pojo.Order;
import com.changgou.order.pojo.OrderItem;
import com.changgou.user.feign.AddressFeign;
import com.changgou.user.pojo.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/worder")
public class OrderController {

    @Autowired
    private CartFeign cartFeign;

    @Autowired
    private AddressFeign addressFeign;

    @Autowired
    private OrderFeign orderFeign;

    @RequestMapping("/ready/order")
    public String readyOrder(Model model){
        //查询地址
        List<Address> addressList = addressFeign.findAddressByUseranem().getData();
        model.addAttribute("addressList",addressList);

        //封装默认收货地址信息
        for (Address address : addressList) {
            if ("1".equals(address.getIsDefault())){
                model.addAttribute("deAddr",address);

                break;
            }
        }

        //购物车信息
        Map map = cartFeign.list();

        List<OrderItem> orderItemList = (List<OrderItem>) map.get("orderItemList");
        Integer totalNum = (Integer) map.get("totalNum");
        Integer totalPrice = (Integer) map.get("totalPrice");

        model.addAttribute("orderItemList",orderItemList);
        model.addAttribute("totalNum",totalNum);
        model.addAttribute("totalPrice",totalPrice);


        return "order";
    }

    /***
     * 新增数据
     * @param order
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestBody Order order){
        Result result = orderFeign.add(order);
        return result;
    }


    /**
     * 跳转支付页面
     * @param orderId
     * @param model
     * @return
     */
    @GetMapping("/toPayPage")
    public String toPayPage(String orderId,Model model){

        Order order = orderFeign.findById(orderId).getData();

        model.addAttribute("orderId",orderId);
        model.addAttribute("payMoney",order.getPayMoney());

        return "pay";
    }


}
